package com.example.atomics;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicMarkableReference;

/**
 * @auther zzyy
 * @create 2021-03-22 14:14
 */
@Slf4j
@SuppressWarnings("unchecked")
public class AtomicMarkableReferenceDemo {
    static AtomicMarkableReference atomicMarkableReference = new AtomicMarkableReference(100, false);

    public static void main(String[] args) {
        new Thread(() -> {
            boolean marked = atomicMarkableReference.isMarked();
            log.info("---默认修改标识：{}", marked);
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                log.error("Exception:", e.fillInStackTrace());
            }
            atomicMarkableReference.compareAndSet(100, 101, marked, !marked);
        }, "t1").start();

        new Thread(() -> {
            boolean marked = atomicMarkableReference.isMarked();
            log.info("---默认修改标识：{}", marked);
            try {
                TimeUnit.SECONDS.sleep(2);
            } catch (InterruptedException e) {
                log.error("Exception:", e.fillInStackTrace());
            }
            boolean b = atomicMarkableReference.compareAndSet(100, 20210308, marked, !marked);
            log.info("---操作是否成功:{}", b);
            log.info("ret:{}", atomicMarkableReference.getReference());
            log.info("是否修改过了,ret:{}", atomicMarkableReference.isMarked());

        }, "t2").start();
    }
}
